package heven.holt.wan.android.extensions

import android.view.View
import androidx.navigation.NavDirections
import androidx.navigation.findNavController
import dagger.hilt.android.EntryPointAccessors
import heven.holt.wan.android.MainFragmentDirections
import heven.holt.wan.android.di.WanUserRepositoryProviderEntryPoint

/**
 * 全局跳转页面拦截，可拦截登录状态
 */
fun View.navigationLoginIntercept(direction: NavDirections) {
    val hiltEntryPoint =
        EntryPointAccessors.fromApplication(
            context.applicationContext,
            WanUserRepositoryProviderEntryPoint::class.java
        )
    val wanUserRepository = hiltEntryPoint.wanUserRepository()
    if (!wanUserRepository.isLogin()) {
        val loginDirection = MainFragmentDirections.actionMainFragmentToLoginFragment()
        findNavController().navigate(loginDirection)
        return
    }
    findNavController().navigate(direction)
}

